{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d737b583-b7a5-4cf2-a787-f3c421169c70",
   "metadata": {},
   "source": [
    "## Python 基础：变量与数据结构\n",
    "\n",
    "### 课程目标\n",
    "\n",
    "本课程旨在帮助学生掌握 Python 中的变量和基本数据结构。学生将学习：\n",
    "\n",
    "- 什么是变量，以及如何定义和使用它们\n",
    "- 变量的命名规则\n",
    "- 常见的数据类型（整数、浮点数、字符串、布尔值）\n",
    "- 主要的数据结构：列表、元组、字典和集合\n",
    "- 每种数据结构的基本操作和使用场景\n",
    "\n",
    "---\n",
    "\n",
    "### 1. 变量\n",
    "\n",
    "#### 1.1 什么是变量？\n",
    "\n",
    "变量是存储数据的载体，每个变量都有一个名称和一个值。在 Python 中，变量通过赋值操作符 `=` 创建和赋值。变量可以看作是内存中的一个“标签”，指向某个具体的值。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ebd5babd-7c90-4476-beba-dd830970bea1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T16:01:41.890107Z",
     "start_time": "2025-02-23T16:01:41.888027Z"
    }
   },
   "outputs": [],
   "source": [
    "# 创建一个名为 x 的变量，并赋值为 5\n",
    "x = 5\n",
    "# 创建一个名为 name 的变量，并赋值为 \"Alice\"\n",
    "name = \"Alice\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a99f304-d542-4f69-b391-49cef04debf9",
   "metadata": {},
   "source": [
    "#### 1.2 如何定义变量？\n",
    "\n",
    "在 Python 中，定义变量的过程非常简单：\n",
    "\n",
    "1. 选择一个合法的变量名（详见命名规则）。\n",
    "2. 使用赋值操作符 `=` 将值赋给变量。\n",
    "3. 不需要显式声明变量的类型，Python 会根据赋值的类型自动推断。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "66b6057d-1f21-4455-a626-b9c8ae4c163d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T16:01:41.899553Z",
     "start_time": "2025-02-23T16:01:41.897855Z"
    }
   },
   "outputs": [],
   "source": [
    "# 定义一个整数变量\n",
    "age = 25\n",
    "# 定义一个字符串变量\n",
    "greeting = \"Hello, World!\"\n",
    "# 定义一个浮点数变量\n",
    "height = 1.75"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "207ceb5e-2437-418e-8074-599761d7661d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T16:01:41.905598Z",
     "start_time": "2025-02-23T16:01:41.903127Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "94c1e529-2b95-496e-9540-4d65c99aa8f1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T16:01:42.014388Z",
     "start_time": "2025-02-23T16:01:42.012729Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1fc94c3e-fb4e-4a9c-aca1-4f1ec7011740",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T16:01:42.025417Z",
     "start_time": "2025-02-23T16:01:42.023487Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(greeting)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3f38f26-9a69-4715-9b6d-1813438d9500",
   "metadata": {},
   "source": [
    "#### 1.3 变量的命名规则\n",
    "\n",
    "变量的命名必须遵循以下规则：\n",
    "\n",
    "- 只能包含字母（a-z, A-Z）、数字（0-9）和下划线（_）。\n",
    "- 不能以数字开头。\n",
    "- 区分大小写（`x` 和 `X` 是不同的变量）。\n",
    "- 不能是 Python 的保留关键字（如 `if`、`for`、`while` 等）。\n",
    "- 建议使用有意义的名称，遵循命名惯例（如使用下划线分隔单词：`my_variable`）。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5a405a3c-d71a-42dc-9f58-8c3b0206a4d2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T16:01:42.061011Z",
     "start_time": "2025-02-23T16:01:42.059138Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid decimal literal (751738969.py, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[6], line 7\u001b[0;36m\u001b[0m\n\u001b[0;31m    2nd_variable = 20  # 错误：不能以数字开头\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid decimal literal\n"
     ]
    }
   ],
   "source": [
    "# 有效的变量名\n",
    "my_variable = 10\n",
    "name2 = \"Bob\"\n",
    "_user_id = 123\n",
    "\n",
    "# 无效的变量名\n",
    "2nd_variable = 20  # 错误：不能以数字开头\n",
    "my-variable = 30   # 错误：不能包含连字符\n",
    "for = 40           # 错误：不能使用保留关键字"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bd8efd0-4cc6-4ae6-bf69-344f2f55b63b",
   "metadata": {},
   "source": [
    "#### 1.4 变量可以做什么？\n",
    "\n",
    "变量可以用来存储和操作数据，包括以下操作：\n",
    "\n",
    "- 算术运算（如加、减、乘、除）\n",
    "- 字符串操作（如拼接、切片）\n",
    "- 逻辑运算（与布尔值相关）\n",
    "- 作为函数的参数或返回值\n",
    "- 存储复杂数据结构的引用（如列表、字典等）\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6b9d6ea0-cb32-4b30-9263-2f83544e5a4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "Alice Smith\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 算术运算\n",
    "x = 5\n",
    "y = 3\n",
    "z = x + y  # z 的值为 8\n",
    "\n",
    "# 字符串拼接\n",
    "first_name = \"Alice\"\n",
    "last_name = \"Smith\"\n",
    "full_name = first_name + \" \" + last_name  # full_name 的值为 \"Alice Smith\"\n",
    "\n",
    "# 逻辑运算\n",
    "is_adult = age >= 18  # age 来自之前的定义，值为 25，结果为 True\n",
    "\n",
    "# 打印结果\n",
    "print(z)           # 输出: 8\n",
    "print(full_name)   # 输出: Alice Smith\n",
    "print(is_adult)    # 输出: True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f986e7fd-be8b-4998-abd1-995d9e5346c7",
   "metadata": {},
   "source": [
    "### 2. 数据类型\n",
    "\n",
    "Python 是一种动态类型语言，变量的类型由其存储的值决定。以下是几种常见的数据类型：\n",
    "\n",
    "#### 2.1 整数（int）\n",
    "\n",
    "用于表示没有小数部分的数字，可以是正数、负数或零。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "93059d7a-8f64-489b-afff-a550c6cb1d37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "age = 25\n",
    "temperature = -5\n",
    "print(type(age))  # 输出: <class 'int'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "cf914246-b818-46db-8376-1f174726b6ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(temperature)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4347d8ab-1c1b-413d-9173-72d9ec1f330d",
   "metadata": {},
   "source": [
    "#### 2.2 浮点数（float）\n",
    "\n",
    "用于表示带有小数部分的数字。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "6a103831-6cbf-446f-a41d-105354affa96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "height = 1.75\n",
    "pi = 3.14159\n",
    "print(type(height))  # 输出: <class 'float'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "a533eb78-396a-4f89-b14d-d419cd3c342e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.14159"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e2d8601-86c2-4b8f-a620-99c5bcd9b2d3",
   "metadata": {},
   "source": [
    "#### 2.3 字符串（str）\n",
    "\n",
    "用于表示文本数据，用单引号 `'` 或双引号 `\"` 括起来。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "80cb53e6-5ff8-4f0a-b830-4b8af4fa1a55",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "name = \"Alice\"\n",
    "greeting = 'Hello, World!'\n",
    "print(type(name))  # 输出: <class 'str'>\n",
    "# 字符串拼接\n",
    "message = \"Hello, \" + name  # 输出: Hello, Alice\n",
    "# 字符串长度\n",
    "length = len(name)  # 输出: 5\n",
    "# 字符串切片\n",
    "first_two = name[:3]  # 输出: Al"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "9be3d43f-d828-421b-83fa-25dcfc57a2fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "message1=greeting+name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "6faafce5-21bb-40ae-88df-d64944e00ef4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Ali'"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_two"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53fecf46-30ad-4cb0-a44d-d52aa4ba9ce5",
   "metadata": {},
   "source": [
    "#### 2.4 布尔值（bool）\n",
    "\n",
    "用于表示真假，只有两个值：`True` 和 `False`。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "77a75b73-a7a7-40f1-abf6-affcc66921fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "is_student = False\n",
    "has_car = False\n",
    "print(type(is_student))  # 输出: <class 'bool'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "15b20e26-6a07-480b-b90a-49fe66b08411",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "# 逻辑运算\n",
    "can_vote = age >= 18 and is_student  # 输出: True\n",
    "print(can_vote)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6dda2d4-29d9-4fcb-9b9d-a194f3ba9aee",
   "metadata": {},
   "source": [
    "### 3. 数据结构\n",
    "\n",
    "数据结构是组织和存储数据的容器。Python 提供了多种内置数据结构，以下是四种最常用的数据结构：列表、元组、字典和集合。\n",
    "\n",
    "#### 3.1 列表（List）\n",
    "\n",
    "##### 3.1.1 什么是列表？\n",
    "\n",
    "列表是有序的可变集合，可以包含不同类型的元素。列表用方括号 `[]` 表示，元素之间用逗号分隔。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "94a4163a-25b5-4547-892c-76972d36df89",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个包含字符串的列表\n",
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "# 创建一个包含整数的列表\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "# 创建一个混合类型的列表\n",
    "mixed = [1, \"hello\", 3.14, True]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9dd14cac-86ef-41d5-8ace-714979e9ca76",
   "metadata": {},
   "source": [
    "##### 3.1.2 访问列表元素\n",
    "\n",
    "列表的元素可以通过索引（从 0 开始）访问。负索引表示从末尾开始计数（-1 是最后一个元素）。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "35fcb68a-7945-47aa-a86e-30ce943dd056",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n",
      "banana\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(fruits[0])   # 输出: apple\n",
    "print(fruits[1])   # 输出: banana\n",
    "print(fruits[-1])  # 输出: cherry\n",
    "print(fruits[-2]) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3423dcb1-b2cc-4cd6-952e-0ddb92662190",
   "metadata": {},
   "source": [
    "##### 3.1.3 修改列表\n",
    "\n",
    "列表是可变的，可以添加、删除或修改元素。\n",
    "\n",
    "**常用操作**：\n",
    "\n",
    "- `append()`：在列表末尾添加元素\n",
    "- `insert()`：在指定位置插入元素\n",
    "- `remove()`：删除指定值的元素\n",
    "- `pop()`：删除并返回指定位置的元素（默认最后一个）\n",
    "- `sort()`：对列表进行排序\n",
    "- `reverse()`：反转列表\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "e5481ab9-284a-4bb6-8a75-81bee12b1d3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry', 'orange']\n",
      "['apple', 'blueberry', 'banana', 'cherry', 'orange']\n",
      "['apple', 'blueberry', 'cherry', 'orange']\n",
      "orange\n",
      "['apple', 'blueberry', 'cherry']\n",
      "[1, 2, 3, 4]\n",
      "[4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "\n",
    "# 添加元素到末尾\n",
    "fruits.append(\"orange\")\n",
    "print(fruits)  # 输出: [\"apple\", \"banana\", \"cherry\", \"orange\"]\n",
    "\n",
    "# 在索引 1 处插入元素\n",
    "fruits.insert(1, \"blueberry\")\n",
    "print(fruits)  # 输出: [\"apple\", \"blueberry\", \"banana\", \"cherry\", \"orange\"]\n",
    "\n",
    "# 删除指定值的元素\n",
    "fruits.remove(\"banana\")\n",
    "print(fruits)  # 输出: [\"apple\", \"blueberry\", \"cherry\", \"orange\"]\n",
    "\n",
    "# 删除并返回最后一个元素\n",
    "last_fruit = fruits.pop()\n",
    "print(last_fruit)  # 输出: orange\n",
    "print(fruits)  # 输出: [\"apple\", \"blueberry\", \"cherry\"]\n",
    "\n",
    "# 对数字列表进行排序\n",
    "numbers = [3, 1, 4, 2]\n",
    "numbers.sort()\n",
    "print(numbers)  # 输出: [1, 2, 3, 4]\n",
    "\n",
    "# 反转列表\n",
    "numbers.reverse()\n",
    "print(numbers)  # 输出: [4, 3, 2, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99b0962f-2d70-48c2-a5a6-08634e62cd4a",
   "metadata": {},
   "source": [
    "##### 3.1.4 列表的切片\n",
    "\n",
    "切片允许你获取列表的一部分。语法为 `list[start:end]`，其中 `start` 是起始索引，`end` 是结束索引（不包含）。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "35ebadc2-b144-4a7d-b5cf-672af11a9afe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[0, 1, 2]\n",
      "[3, 4, 5]\n",
      "[4, 5]\n"
     ]
    }
   ],
   "source": [
    "numbers = [0, 1, 2, 3, 4, 5]\n",
    "print(numbers[1:4])  # 输出: [1, 2, 3]\n",
    "print(numbers[:3])   # 输出: [0, 1, 2]\n",
    "print(numbers[3:])   # 输出: [3, 4, 5]\n",
    "print(numbers[-2:])  # 输出: [4, 5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1da5eea7-72bc-4cbb-a6a8-4a63a2a8e219",
   "metadata": {},
   "source": [
    "#### 3.2 元组（Tuple）\n",
    "\n",
    "##### 3.2.1 什么是元组？\n",
    "\n",
    "元组是有序的不可变集合，一旦创建就不能修改。元组用圆括号 `()` 表示，元素之间用逗号分隔。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "a581b9c3-3b91-4e6f-948d-39f03bd74f94",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个元组\n",
    "coordinates = (3, 4)\n",
    "# 创建一个单元素元组（注意逗号）\n",
    "single = (5,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "1b1ef5c6-9d2e-4310-ba26-7dd29c5718af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eeea1bc8-e8df-4c9c-89a7-7d4c0ac602f9",
   "metadata": {},
   "source": [
    "##### 3.2.2 访问元组元素\n",
    "\n",
    "元组的元素可以通过索引访问，类似于列表。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c59fc23f-a411-4760-ad91-a50a82abe6a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "coordinates = (3, 4)\n",
    "print(coordinates[0])  # 输出: 3\n",
    "print(coordinates[1])  # 输出: 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9a62eb5-fc11-4342-b3df-6273da29a056",
   "metadata": {},
   "source": [
    "##### 3.2.3 元组的不可变性\n",
    "\n",
    "元组是不可变的，尝试修改元组的元素会引发错误。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "db68e1ed-2d80-4eaf-85b1-83ca110ef548",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[149], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m coordinates \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m coordinates[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m5\u001b[39m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "coordinates = (3, 4)\n",
    "# coordinates[0] = 5  # TypeError: 'tuple' object does not support item assignment"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4531538-c953-4d99-9f36-e7f666b04ab4",
   "metadata": {},
   "source": [
    "##### 3.2.4 元组的应用场景\n",
    "\n",
    "- 存储不应更改的数据，如坐标、RGB 颜色值\n",
    "- 作为字典的键（因为元组是可哈希的）\n",
    "- 返回多个值\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "bdb43370-a0c7-4e3e-85d1-a85265f2acc7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "def get_point():\n",
    "    return (3, 4)\n",
    "\n",
    "point = get_point()\n",
    "print(point)  # 输出: (3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14aa415a-3463-4c0f-97c3-a2b80824f295",
   "metadata": {},
   "source": [
    "#### 3.3 字典（Dictionary）\n",
    "\n",
    "##### 3.3.1 什么是字典？\n",
    "\n",
    "字典是无序的键值对集合，键必须是唯一的。字典用花括号 `{}` 表示，键和值之间用冒号 `:` 分隔。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "0846f559-40ba-4056-b820-28240c55e2bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个字典\n",
    "student = {\"name\": \"Alice\", \"age\": 20, \"major\": \"Computer Science\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c206070-2f77-4832-86e6-d1db76c319ce",
   "metadata": {},
   "source": [
    "##### 3.3.2 访问字典元素\n",
    "\n",
    "通过键来访问对应的值。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "249279fd-3b23-4362-b8d9-278474bae901",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "print(student[\"name\"])  # 输出: Alice\n",
    "print(student[\"age\"])   # 输出: 20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "325fd1b7-5257-4f3f-9023-bf77e7d50231",
   "metadata": {},
   "source": [
    "##### 3.3.3 修改字典\n",
    "\n",
    "字典是可变的，可以添加、修改或删除键值对。\n",
    "\n",
    "**常用操作**：\n",
    "\n",
    "- 添加/修改：`dict[key] = value`\n",
    "- 删除：`del dict[key]`\n",
    "- `keys()`：返回所有键\n",
    "- `values()`：返回所有值\n",
    "- `items()`：返回所有键值对\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "b8e7e967-14ff-46b7-acc3-e509f54ff869",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Alice', 'age': 21, 'grade': 'A'}\n",
      "{'name': 'Alice', 'grade': 'A'}\n",
      "dict_keys(['name', 'grade'])\n",
      "dict_values(['Alice', 'A'])\n",
      "dict_items([('name', 'Alice'), ('grade', 'A')])\n"
     ]
    }
   ],
   "source": [
    "student = {\"name\": \"Alice\", \"age\": 20}\n",
    "student[\"age\"] = 21        # 修改现有键的值\n",
    "student[\"grade\"] = \"A\"     # 添加新键值对\n",
    "print(student)  # 输出: {\"name\": \"Alice\", \"age\": 21, \"grade\": \"A\"}\n",
    "\n",
    "del student[\"age\"]  # 删除键 \"age\"\n",
    "print(student)  # 输出: {\"name\": \"Alice\", \"grade\": \"A\"}\n",
    "\n",
    "print(student.keys())    # 输出: dict_keys(['name', 'grade'])\n",
    "print(student.values())  # 输出: dict_values(['Alice', 'A'])\n",
    "print(student.items())   # 输出: dict_items([('name', 'Alice'), ('grade', 'A')])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26f808af-b8a6-43a9-8fd1-07957a69134c",
   "metadata": {},
   "source": [
    "##### 3.3.4 字典的应用场景\n",
    "\n",
    "- 存储和快速检索数据，如电话簿、配置文件\n",
    "- 计数器，如统计单词出现次数\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "37373e48-68b0-4aa1-8d12-d90ffd4a4c9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 1, 2: 2, 3: 3}\n"
     ]
    }
   ],
   "source": [
    "# 统计列表中元素的出现次数\n",
    "numbers = [1, 2, 2, 3, 3, 3]\n",
    "count = {}\n",
    "for num in numbers:\n",
    "    if num in count:\n",
    "        count[num] += 1\n",
    "    else:\n",
    "        count[num] = 1\n",
    "print(count)  # 输出: {1: 1, 2: 2, 3: 3}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ed1b027-1aeb-47f0-9059-3774e09f5360",
   "metadata": {},
   "source": [
    "#### 3.4 集合（Set）\n",
    "\n",
    "##### 3.4.1 什么是集合？\n",
    "\n",
    "集合是无序的唯一元素集合，自动去除重复项。集合用花括号 `{}` 表示。\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "87bf36cd-ee35-4210-afae-bc2ea48b0840",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "# 创建一个集合\n",
    "fruits = {\"apple\", \"banana\", \"cherry\", \"apple\"}\n",
    "print(fruits)  # 输出: {\"apple\", \"banana\", \"cherry\"}（自动去重）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd70dc65-16db-450c-a93f-1399765de653",
   "metadata": {},
   "source": [
    "##### 3.4.2 集合的操作\n",
    "\n",
    "集合支持数学集合操作，如并集、交集、差集等。\n",
    "\n",
    "**常用操作**：\n",
    "\n",
    "- `add()`：添加元素\n",
    "- `remove()`：删除元素\n",
    "- `union()` 或 `|`：并集\n",
    "- `intersection()` 或 `&`：交集\n",
    "- `difference()` 或 `-`：差集\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d5a5c35c-8cb2-42dc-a2b1-db84b9348537",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5}\n",
      "{3}\n",
      "{1, 2}\n",
      "{1, 2, 3, 4}\n",
      "{1, 3, 4}\n"
     ]
    }
   ],
   "source": [
    "set1 = {1, 2, 3}\n",
    "set2 = {3, 4, 5}\n",
    "\n",
    "# 并集\n",
    "print(set1 | set2)  # 输出: {1, 2, 3, 4, 5}\n",
    "\n",
    "# 交集\n",
    "print(set1 & set2)  # 输出: {3}\n",
    "\n",
    "# 差集\n",
    "print(set1 - set2)  # 输出: {1, 2}\n",
    "\n",
    "# 添加元素\n",
    "set1.add(4)\n",
    "print(set1)  # 输出: {1, 2, 3, 4}\n",
    "\n",
    "# 删除元素\n",
    "set1.remove(2)\n",
    "print(set1)  # 输出: {1, 3, 4}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e8f0adb-f5d9-46cb-917c-cd65f343a1cd",
   "metadata": {},
   "source": [
    "##### 3.4.3 集合的应用场景\n",
    "\n",
    "- 去除列表中的重复项\n",
    "- 快速成员测试\n",
    "- 集合运算，如查找共同元素\n",
    "\n",
    "**示例**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3a981d55-a1f9-4605-b66a-d70256426d9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5}\n",
      "3 is in the set\n"
     ]
    }
   ],
   "source": [
    "# 去除列表中的重复项\n",
    "numbers = [1, 2, 2, 3, 4, 4, 5]\n",
    "unique_numbers = set(numbers)\n",
    "print(unique_numbers)  # 输出: {1, 2, 3, 4, 5}\n",
    "\n",
    "# 成员测试\n",
    "if 3 in unique_numbers:\n",
    "    print(\"3 is in the set\")  # 输出: 3 is in the set"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75b7ecc3-4412-480a-bf8b-8514fc0856f1",
   "metadata": {},
   "source": [
    "### 4. 总结\n",
    "\n",
    "本课程介绍了 Python 中的变量和基本数据结构：\n",
    "\n",
    "- **变量**：用于存储数据，遵循特定的命名规则。\n",
    "- **数据类型**：包括整数、浮点数、字符串和布尔值。\n",
    "- **列表**：有序可变集合，支持多种操作。\n",
    "- **元组**：有序不可变集合，适合存储固定数据。\n",
    "- **字典**：无序键值对集合，适合快速查找。\n",
    "- **集合**：无序唯一元素集合，适合去重和集合运算。\n",
    "\n",
    "通过本课程的学习，学生应该能够理解和使用这些基础概念，为进一步学习 Python 打下坚实的基础。\n",
    "\n",
    "---\n",
    "\n",
    "### 5. 练习\n",
    "\n",
    "1. **变量练习**：\n",
    "   - 创建一个变量存储你的姓名，并打印出来。\n",
    "   - 创建一个变量存储你的年龄，并打印出来。\n",
    "\n",
    "2. **列表练习**：\n",
    "   - 创建一个包含三种水果的列表。\n",
    "   - 添加一种新水果到列表末尾。\n",
    "   - 删除列表中的第二种水果。\n",
    "   - 打印列表的第一个和最后一个元素。\n",
    "\n",
    "3. **元组练习**：\n",
    "   - 创建一个包含你的姓名和年龄的元组。\n",
    "   - 尝试修改元组的年龄（会报错），并理解错误信息。\n",
    "\n",
    "4. **字典练习**：\n",
    "   - 创建一个字典，包含你的姓名、年龄和专业。\n",
    "   - 添加一个新键值对，表示你的年级。\n",
    "   - 修改你的年龄。\n",
    "   - 删除专业键值对。\n",
    "   - 打印字典的所有键和值。\n",
    "\n",
    "5. **集合练习**：\n",
    "   - 创建一个包含重复数字的列表。\n",
    "   - 将列表转换为集合以去除重复项。\n",
    "   - 创建另一个集合，并与第一个集合进行并集、交集和差集操作。\n",
    "\n",
    "---\n",
    "\n",
    "### 6. 资源\n",
    "\n",
    "- [Python 官方文档](https://docs.python.org/3/)\n",
    "- [GeeksforGeeks Python 教程](https://www.geeksforgeeks.org/python-programming-language/)\n",
    "- [Real Python](https://realpython.com/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b04d9bf9-086d-43a4-bacb-c5f37f22078e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
